import apiClient from './api'

// 分片任务相关的类型定义
export interface ShardStatusResponse {
  job_id: string
  total_shards: number
  active_nodes: number
  node_list: string[]
  shards: Array<{
    shard_index: number
    node_id: string
    status: string
    created_at: string
    expire_time: string
  }>
  status: string
}

export interface ShardExecutionRequest {
  handler_name: string
  shard_count: number
  shard_strategy: string
  parameters: Record<string, any>
}

export interface ShardOptimizationRequest {
  data_size: number
  available_nodes?: number
  processing_time_per_item?: number
}

export interface ShardOptimizationResponse {
  recommended_shards: number
  estimated_time_per_shard: number
  estimated_total_time: number
  utilization_rate: number
}

export interface ClusterNode {
  node_id: string
  active_shards: number
  first_shard_time?: string
  last_expire_time?: string
  processing_jobs: string[]
  status: string
}

export interface ClusterNodesResponse {
  total_nodes: number
  total_active_shards: number
  nodes: ClusterNode[]
  timestamp: string
}

export interface ShardingStrategy {
  name: string
  description: string
  use_case: string
}

export interface ShardHandler {
  name: string
  description: string
  parameters: Record<string, string>
}

// 分片任务管理API服务
export const shardTaskApi = {
  // 获取分片状态
  getShardStatus: (jobId: string): Promise<ShardStatusResponse> => {
    return apiClient.get(`/shard-tasks/${jobId}/status`)
  },

  // 触发分片执行
  triggerShardExecution: (jobId: string, data: ShardExecutionRequest): Promise<any> => {
    return apiClient.post(`/shard-tasks/${jobId}/trigger-shard`, data)
  },

  // 分片优化建议
  getShardOptimization: (data: ShardOptimizationRequest): Promise<ShardOptimizationResponse> => {
    return apiClient.post('/shard-tasks/optimize', data)
  },

  // 获取分片策略
  getShardingStrategies: (): Promise<{ strategies: ShardingStrategy[] }> => {
    return apiClient.get('/shard-tasks/strategies')
  },

  // 获取分片处理器
  getShardHandlers: (): Promise<{ 
    handlers: ShardHandler[]
    total_handlers: number
    registered_handlers: string[]
  }> => {
    return apiClient.get('/shard-tasks/handlers')
  },

  // 获取集群节点
  getClusterNodes: (): Promise<ClusterNodesResponse> => {
    return apiClient.get('/shard-tasks/cluster/nodes')
  },

  // 清理完成的分片
  cleanupCompletedShards: (jobId?: string): Promise<{
    message: string
    cleaned_count: number
    job_id: string
  }> => {
    const params = jobId ? { job_id: jobId } : {}
    return apiClient.delete('/shard-tasks/cleanup/completed', { params })
  },
}